package com.txsj.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;

import com.txsj.scan.AppManager;
import com.txsj.scan.R;
import com.txsj.scan.adapter.SelectCompanyAdapter;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.bean.CompanyBean;
import com.txsj.scan.presenter.SelectCompanyPresenter;
import com.txsj.scan.refresh.XRefreshView;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.view.ISelectCompanyView;
import com.txsj.scan.widget.TitleBarView;
import com.txsj.scan.widget.swipemenu.SwipeRecyView;

import java.util.List;

/**
 * Created by wangchuangjun on 2019/2/20
 * Desc: 选择企业列表
 */

public class SelectCompanyActivity extends BaseActivity<SelectCompanyPresenter> implements ISelectCompanyView {

    XRefreshView xRefreshView;
    SwipeRecyView recyclerView;
    SelectCompanyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_company);
        initView();
        initData();
    }

    private void initData() {
        presenter.getCompanyList();
    }

    private void initView() {
        mTitle = (TitleBarView) findViewById(R.id.mTitle);
        xRefreshView = (XRefreshView) findViewById(R.id.xRefreshView);
        xRefreshView.setAutoLoadMore(false);
        xRefreshView.setPullRefreshEnable(false);
        xRefreshView.setAutoRefresh(false);
        initEmptyView();
        recyclerView = (SwipeRecyView) findViewById(R.id.recyclerView);
        adapter = new SelectCompanyAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        adapter.setListener(new SelectCompanyAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                CompanyBean companyBean = adapter.getList().get(position);
                Intent intent = new Intent(SelectCompanyActivity.this, SelectGoodsListActivity.class);
                startActivity(intent);
                if (AppManager.isActivityExist(LoginActivity.class)) {
                    AppManager.getActivity(LoginActivity.class).finish();
                }
                //选择完企业后才算登陆成功
                PreferenceUtil.getInstance().commitString(PreferenceUtil.BRAND_ID, companyBean.getBrandId());
                PreferenceUtil.getInstance().commitString(PreferenceUtil.BRAND_NAME, companyBean.getBrandName());
                PreferenceUtil.getInstance().commitString(PreferenceUtil.USER_NAME, companyBean.getUserName());
                PreferenceUtil.getInstance().commitString(PreferenceUtil.ROLE, companyBean.getRole());
                PreferenceUtil.getInstance().commitString(PreferenceUtil.USER_NAME, companyBean.getUserName());
                PreferenceUtil.getInstance().commitString(PreferenceUtil.LOGO, companyBean.getLogo());
                PreferenceUtil.getInstance().commitInt(PreferenceUtil.USER_ID, companyBean.getUser_id());
                PreferenceUtil.getInstance().commitString(PreferenceUtil.TOKEN, companyBean.getToken());
                PreferenceUtil.getInstance().commitString(PreferenceUtil.COMID, companyBean.getComid());
                PreferenceUtil.getInstance().commitInt(PreferenceUtil.EMPLOYEEID, companyBean.getEmployeeId());
                PreferenceUtil.getInstance().commitString(PreferenceUtil.EMPLOYEENAME, companyBean.getEmployeeName());
                PreferenceUtil.getInstance().commitBoolean(PreferenceUtil.IS_LOGINED, true);
                finish();
            }
        });

    }

    @Override
    protected SelectCompanyPresenter providePresenter() {
        return new SelectCompanyPresenter(this);
    }

    @Override
    public Context context() {
        return this;
    }

    @Override
    public void onSuccess(List<CompanyBean> data) {
        hideLoading();
        adapter.setList(data);
        xRefreshView.enableEmptyView(false);
    }

    @Override
    public void onEmpty() {
        hideLoading();
        showErrorMsg("暂无数据");
        xRefreshView.enableEmptyView(true);
    }

    @Override
    public void onLoading() {
        showLoading();
    }

    @Override
    public void onError(String msg) {
        hideLoading();
        showErrorMsg(msg);
        xRefreshView.enableEmptyView(true);
    }

    Button btn_empty_reload;

    /**
     * 列表数据为空时 显示的View
     */
    private void initEmptyView() {
        View emptyView = View.inflate(this, R.layout.view_empty, null);
        btn_empty_reload = (Button) emptyView.findViewById(R.id.btn_empty_reload);
        xRefreshView.setEmptyView(emptyView);
        btn_empty_reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.getCompanyList();
            }
        });
    }
}
